import java.util.Arrays;

public class Test3 {
    //给定一个有序整型数组, 实现二分查找

    public static int myBrinarySearch(int[] arr, int key) {//数组从小到大排列
        int len = arr.length;

        int left = 0;
        int right = len - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (arr[mid] > key) {
                right = mid - 1;
            } else if (arr[mid] < key) {
                left = mid + 1;
            } else {
                return mid;
            }
        }

        return -1;
    }
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4};

        System.out.println(Arrays.toString(arr));

        int ret = myBrinarySearch(arr, 5);

        System.out.println(ret);
    }
}
